using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;

using Pampa.Grid.Exceptions;

namespace Pampa.Grid.Core.Actions
{
    public class Action
    {
        private string text;
        private string methodName;

        public Action()
        { 
        }

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public string MethodName
        {
            get { return methodName; }
            set { methodName = value; }
        }

        public virtual void LoadXml(XmlNode node)
        {
            if (node.Attributes["Text"] != null)
                this.Text = node.Attributes["Text"].Value;

            if (node.Attributes["MethodName"] != null)
                this.MethodName = node.Attributes["MethodName"].Value;
            else
                throw (new ActionConfigurationException("No method was defined for ItemAction"));
        }

        public static Action LoadFromXml(XmlNode node)
        {
            Assembly assembly = Assembly.GetAssembly(typeof(ItemAction));
            object objItemAction = assembly.CreateInstance("Pampa.Grid.Core.Actions." + node.Name);

            if (objItemAction is Action)
            {
                Action itemAction = (Action)objItemAction;
                itemAction.LoadXml(node);
                return itemAction;
            }
            else
            {
                throw (new ActionConfigurationException("There is no Action of Type " + node.Name));
            }
        }
    }
}
